/* 用于存放频道相关的数据 */
import { getChannelList, getAllChannelList } from '@/api/channel'
export default {
  namespaced: true,
  state: {
    channelList: [],
    allChannelList: []
  },
  mutations: {
    setChannelList (state, payload) {
      state.channelList = payload
    },
    setAllChannelList (state, payload) {
      state.allChannelList = payload
    },
    addChannel (state, channel) {
      // 添加频道
      state.channelList.push(channel)
    },
    delChannel (state, channel) {
      state.channelList = state.channelList.filter(item => +item.id !== +channel.id)
    }
  },
  actions: {
    async getChannelList(context) {
      const res = await getChannelList()
      console.log(res.data.channels)
      context.commit('setChannelList', res.data.channels)
    },
    async getAllChannelList(context) {
      const res = await getAllChannelList()
      context.commit('setAllChannelList', res.data.channels)
    }
  },
  getters: {
    optionalChannelList (state) {
      return state.allChannelList
        .filter(item => state.channelList.findIndex(v => +v.id === +item.id) === -1)
    }
    // 可选的频道的列表
    // optionalChannelList (state) {
    //   // 全部频道 - 我的频道
    //   return state.allChannelList.filter((item) => {
    //     // 如果返回true，元素就会保留
    //     // 判断item在channelList中是否存在
    //     return state.channelList.findIndex(v => v.id === item.id) === -1
    //     // if (idx === -1) {
    //     //   return true
    //     // } else {
    //     //   return false
    //     // }
    //     // return idx === -1
    //   })
    // }
  }
}
